package com.mccken.designpattern.state.sub;

import com.mccken.designpattern.state.GumballMachine;
import com.mccken.designpattern.state.State;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2023-02-26 15:49
 **/
public class WinnerState implements State {
	GumballMachine gumballMachine;
	
	public WinnerState(GumballMachine gumballMachine) {
		this.gumballMachine = gumballMachine;
	}
	
	@Override
	public void insertQuarter() {
		System.out.println("WinnerState insertQuarter");
	}

	@Override
	public void ejectQuarter() {
		System.out.println("WinnerState ejectQuarter");
	}

	@Override
	public void turnCrank() {
		System.out.println("WinnerState turnCrank");
	}

	@Override
	public void dispense() {
		System.out.println("WinnerState dispense");
		gumballMachine.releaseBall();
		
		if (gumballMachine.getCount() == 0) {
			gumballMachine.setState(gumballMachine.getSoldOutState());
		} else {
			gumballMachine.releaseBall();
			if (gumballMachine.getCount() > 0) {
				gumballMachine.setState(gumballMachine.getNoQuarterState());
			} else {
				System.out.println("Oop, out of gumballs!");
				gumballMachine.setState(gumballMachine.getSoldOutState());
			}
			
		}
	}
}
